package com.whfc.emp.service.impl;

import cn.hutool.core.collection.CollUtil;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.whfc.common.exception.BizException;
import com.whfc.common.result.PageData;
import com.whfc.common.result.ResultEnum;
import com.whfc.common.util.PageUtil;
import com.whfc.emp.dao.AppTrainingMapper;
import com.whfc.emp.dao.AppTrainingQuestionMapper;
import com.whfc.emp.dto.AppTrainingCategoryDTO;
import com.whfc.emp.dto.AppTrainingDTO;
import com.whfc.emp.dto.AppTrainingQuestionsDTO;
import com.whfc.emp.entity.AppTraining;
import com.whfc.emp.entity.AppTrainingQuestion;
import com.whfc.emp.param.*;
import com.whfc.emp.service.AppTrainingCategoriesService;
import com.whfc.emp.service.AppTrainingQuestionsService;
import com.whfc.emp.service.AppTrainingService;
import com.whfc.fuum.service.SysDeptService;
import com.whfc.fuum.service.SysDictService;
import org.apache.dubbo.config.annotation.DubboReference;
import org.apache.dubbo.config.annotation.DubboService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;

import java.util.Collections;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * @ClasssName AppTrainServiceImpl
 * @Description 人员管理-培训
 * @Author hw
 * @Date 2020/11/26 16:18
 * @Version 1.0
 */
@DubboService(interfaceClass = AppTrainingQuestionsService.class, version = "1.0.0", timeout = 5000)
public class AppTrainingQuestionsServiceImpl implements AppTrainingQuestionsService {

    @Autowired
    private AppTrainingQuestionMapper appTrainingQuestionMapper;
    @Override
    public PageData<AppTrainingQuestionsDTO> list(AppTrainingQuestionListParam request) {
        PageHelper.startPage(request.getPageNum(), request.getPageSize());
        List<AppTrainingQuestionsDTO> list = appTrainingQuestionMapper.selectByParam(request);
        list.forEach(item -> {
            item.setOptions(appTrainingQuestionMapper.selectOptionsByQuestionId(item.getId()));
        });
        PageHelper.clearPage();
        return PageUtil.pageData(PageInfo.of(list));
    }

    @Override
    public void add(AppTrainingQuestionAddParam request) throws BizException {

    }

    @Override
    public void edit(AppTrainingQuestionEditParam request) throws BizException {

    }

    @Override
    public void del(Integer id) throws BizException {

    }

    @Override
    public Map<Integer, AppTrainingQuestionsDTO> getQuestionMap(List<Integer> questionIds) {
        List<AppTrainingQuestionsDTO> list = appTrainingQuestionMapper.selectByIds(questionIds);
        list.forEach(item -> {
            item.setOptions(appTrainingQuestionMapper.selectOptionsByQuestionId(item.getId()));
        });
        return list.stream().collect(Collectors.toMap(AppTrainingQuestionsDTO::getId, item-> item));
    }

    @Override
    public void saveBatchByTrainingId(List<AppTrainingQuestion> questions, Long id) {
        if(CollUtil.isEmpty( questions)){
            throw new BizException(ResultEnum.PARAM_ERROR, "问题明细为空");
        }
        questions.forEach(item -> {
            item.setTrainingId(id);
        });
        appTrainingQuestionMapper.insertBatchByTrainingId(questions);
    }

    @Override
    public Integer countByTrainingId(Integer id) {
        return appTrainingQuestionMapper.countByTrainingId(id);
    }
}
